import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from sklearn import feature_extraction
from sklearn.cross_validation import train_test_split



from sklearn import feature_extraction
def one_hot_dataframe(data, cols, replace=False):
    print data   
    print cols   
    vec = feature_extraction.DictVectorizer()
    print vec   

    mkdict = lambda row: dict((col, row[col]) for col in cols)
    print mkdict   
    print cols
  
    #vecData = pd.DataFrame(vec.fit_transform(data[cols].apply(mkdict, axis=1)).toarray())
    vecData = pd.DataFrame(vec.fit_transform(data[cols].to_dict(outtype='records')).toarray())
    print vecData   
    raw_input("Press enter to continue")
    vecData.columns = vec.get_feature_names()
    vecData.index = data.index
    if replace:
        data = data.drop(cols, axis=1)
        data = data.join(vecData)
    print data
    raw_input("Press enter to continue")
    return (data, vecData)


def main(): 
    ids = pd.read_csv('titanic.csv')
    ids2,ids_n = one_hot_dataframe(ids, ['pclass', 'embarked', 'sex'], replace=True)
    #ids2.describe()
    ids2,ids_n = one_hot_dataframe(ids, ['pclass', 'embarked', 'sex'], replace=True)
    #ids2.describe()


if __name__ == "__main__":
    main()

